package com.jade.editor.domain.parts;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class SettingsAni extends Dialog {

	public static final String ANI_TYPE_DRAWABLE = "DRAWABLE";
	public static final String ANI_TYPE_PLUGABLE = "PLUGABLE";
	
	private static final String SOLVE_ID = "com.jade.domain.solve";
	
	private File _aniDir = null;
	private File[] _aniList = null;
	private String _selectedAni = "";
	private String _lastUsed = "";
	private String _lastType = "";
	private String _pluginLocation = " ";
	private ArrayList<String> _pluginList = new ArrayList<String>();
	private HashMap<String, String> _pluginMap = new HashMap<String, String>();
	
	public File getSelectedAni(){
		for(File f : _aniList){
			if (f.getName().equals(_selectedAni))
				return f;
		}
		return null;
	}
	
	public String getPluginLocation(){
		return _pluginLocation;
	}
	
	public String getLastUsedName(){
		return _lastUsed;
	}
	
	
	public String getLastUsedType(){
		return _lastType;
	}
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsAni(Shell parentShell, IPath path, String lastUsed, String lastType) {
		super(parentShell);
		_lastUsed = lastUsed;
		
		if (lastType.equals(SettingsAni.ANI_TYPE_DRAWABLE) || lastType.equals(SettingsAni.ANI_TYPE_PLUGABLE))
			_lastType = lastType;
		else
			_lastType = SettingsAni.ANI_TYPE_DRAWABLE;
		
		_aniDir = new File(path.toFile().getParentFile().getParent() + "//Anisotropy");
		
		FileFilter aniFilter = new FileFilter(){

			@Override
			public boolean accept(File f) {
				if(f.getPath().endsWith("ani"))
					return true;
				return false;
			}
			
		};
		
		_aniList = _aniDir.listFiles(aniFilter);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		//---------------------------DRAWABLE
		
		final Group grpDrawableAnisotropy = new Group(container, SWT.NONE);
		grpDrawableAnisotropy.setText("Drawable Anisotropy");
		grpDrawableAnisotropy.setBounds(26, 10, 255, 80);
		
		Label lblSelectAnisotropy = new Label(grpDrawableAnisotropy, SWT.NONE);
		lblSelectAnisotropy.setBounds(10, 23, 229, 15);
		lblSelectAnisotropy.setText("Select Anisotropy:");
		
		final Button btnDraw = new Button(container, SWT.RADIO);
		btnDraw.setLocation(10, 10);
		btnDraw.setSize(15, 16);
		btnDraw.setText("draw");
		
		final Combo comboDraw = new Combo(grpDrawableAnisotropy, SWT.NONE);
		comboDraw.setBounds(10, 44, 229, 23);
		
		for(File f : _aniList){
			comboDraw.add(f.getName());
		}
	
		//----------------------------PLUGABLE
		
		final Button btnPlug = new Button(container, SWT.RADIO);
		btnPlug.setBounds(10, 98, 17, 16);
		btnPlug.setText("Plug");
		
		final Group grpPlugableAnisotropy = new Group(container, SWT.NONE);
		grpPlugableAnisotropy.setText("Plugable Anisotropy");
		grpPlugableAnisotropy.setBounds(26, 96, 255, 82);	
		
		Label lblSelectAnisotropy_1 = new Label(grpPlugableAnisotropy, SWT.NONE);
		lblSelectAnisotropy_1.setBounds(10, 23, 95, 15);
		lblSelectAnisotropy_1.setText("Select Anisotropy:");
		
		//---Plugins
		IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(SOLVE_ID);
		
		try{
			
			for (IConfigurationElement e : config) {
				final Object o = e.createExecutableExtension("class");
				if (o instanceof ISolve) {
					
					ISafeRunnable runnable = new ISafeRunnable() {
						@Override
						public void handleException(Throwable exception) {
							System.out.println("Exception in client");
						}

						@Override
						public void run() throws Exception {
							_pluginList.add(((ISolve) o).getName());
							_pluginMap.put(((ISolve) o).getName(), o.getClass().getProtectionDomain().getCodeSource().getLocation().toString());
						}
					};
					SafeRunner.run(runnable);
				}
			}
		}
		catch(CoreException ex){
			System.out.println(ex.getMessage());
		}
		//---Plugins
		
		final Combo comboPlug = new Combo(grpPlugableAnisotropy, SWT.NONE);
		comboPlug.setBounds(10, 44, 235, 23);
		
		for(String plugin : _pluginList){
			comboPlug.add(plugin);
		}
		
		//--------------------------------------
		
		if (comboPlug.getItemCount() >= 0 ){
			comboPlug.select(0);
			if (_pluginMap.size() > 0)
				_pluginLocation = _pluginMap.get(comboPlug.getItem(comboPlug.getSelectionIndex()));
		}
		
		if (comboDraw.getItemCount() >= 0 ){
			comboDraw.select(0);
			_selectedAni = comboDraw.getItem(comboDraw.getSelectionIndex());
		}
		
		if(_lastType.equals(SettingsAni.ANI_TYPE_DRAWABLE)){
			
			btnDraw.setSelection(true);
			grpDrawableAnisotropy.setEnabled(true);
			comboDraw.setEnabled(true);
			
			btnPlug.setSelection(false);
			grpPlugableAnisotropy.setEnabled(false);
			comboPlug.setEnabled(false);
			
			if (!_lastUsed.equals("")){
				for(int i = 0; i < comboDraw.getItemCount(); i++){
					if (comboDraw.getItem(i).equals(_lastUsed))
						comboDraw.select(i);
				}
			}
			
			_lastUsed = comboDraw.getItem(comboDraw.getSelectionIndex());
			
		}
		else if (_lastType.equals(SettingsAni.ANI_TYPE_PLUGABLE)){
			
			btnDraw.setSelection(false);
			grpDrawableAnisotropy.setEnabled(false);
			comboDraw.setEnabled(false);
			
			btnPlug.setSelection(true);
			grpPlugableAnisotropy.setEnabled(true);
			comboPlug.setEnabled(true);
			
			for(int i = 0; i < comboPlug.getItemCount(); i++){
				if (comboPlug.getItem(i).equals(_lastUsed))
					comboPlug.select(i);
			}
			
			_lastUsed = comboPlug.getItem(comboPlug.getSelectionIndex());
			_pluginLocation = _pluginMap.get(_lastUsed);
		}
		
		
		//--------------------------------------LISTENERS
		
		btnDraw.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				btnDraw.setSelection(true);
				grpDrawableAnisotropy.setEnabled(true);
				comboDraw.setEnabled(true);
				
				btnPlug.setSelection(false);
				grpPlugableAnisotropy.setEnabled(false);
				comboPlug.setEnabled(false);
				
				_lastType = SettingsAni.ANI_TYPE_DRAWABLE;
				_lastUsed = comboDraw.getItem(comboDraw.getSelectionIndex());
			}
		});
		
		btnPlug.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				btnDraw.setSelection(false);
				grpDrawableAnisotropy.setEnabled(false);
				comboDraw.setEnabled(false);
				
				btnPlug.setSelection(true);
				grpPlugableAnisotropy.setEnabled(true);
				comboPlug.setEnabled(true);
				
				_lastType = SettingsAni.ANI_TYPE_PLUGABLE;
				if (comboPlug.getItemCount() > 0){
					_lastUsed = comboPlug.getItem(comboPlug.getSelectionIndex());
					_pluginLocation = _pluginMap.get(_lastUsed);
				}
				
			}
		});
		
		comboDraw.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (comboDraw.getSelectionIndex() >= 0){
					_selectedAni = comboDraw.getItem(comboDraw.getSelectionIndex());
					_lastUsed = _selectedAni;
				}
			}
		});
		
		comboPlug.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (comboPlug.getSelectionIndex() >= 0){
					_lastUsed = comboPlug.getItem(comboPlug.getSelectionIndex());
					_pluginLocation = _pluginMap.get(_lastUsed);
				}
			}
		});
		
		
		

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(307, 272);
	}
}